<?php
/**
 * 转写文件
 */

namespace app\script\controller;

use app\common\controller\Common;
use app\api\model\LiveSpeech1;

class Speech extends Common
{
    protected $model;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new LiveSpeech1;
    }

        /**
     * 检查回收站的文件，删除超过十天以上的文件
     */
    public function timeoToDel()
    {
        if (IS_CLI) {
            //删除时间差值
            $time = time() - 3600 * 24 * 20;
            $speech = Model("Speech");
            $all = $speech->where(['is_delete' => 1])->where('create_time', '<', $time)->select();
            foreach ($all as $delete) {
                $folder = $delete['meeting_id'];
                $filename = str_replace(strrchr($delete->formatname, "."), ".mp3", $delete->formatname);
                $jsonname = str_replace(strrchr($delete->formatname, "."), ".json", $delete->formatname);
                //检查文件是否存在
                $filepath = config('share_path') . config('huisheng.file_mp3_path') . '/' . $folder . '/' . $filename;
                $jsonpath = config('share_path') . config('huisheng.file_mp3_path') . '/' . $folder . '/' . $jsonname;
                //删除操作 操作成功删除文件
                $delete->save(['is_delete' => 2]);
                is_file($filepath) ? @unlink($filepath) : '';
                is_file($jsonpath) ? @unlink($jsonpath) : '';
            }
            return 'ok';
        }
        //记录日志
        return 'fail';
    }

    public function pushCompleteSpeechFiel()
    {
        //脚本文件 必须在cli下执行
        //if (IS_CLI) {
            try {
                $list = $this->model->getEndSpeechList();
                var_dump($list);
                while(null != ($id = array_pop($list))){
                    $result = self::$redis->pushEndSpeechQueue($id);
                }
                return 1;
            }catch (\Exception $e) {
                //记录日志
                return 0;
            }
        //}
    }
}